package net.caixiaomi.info.adapter;

import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.MatchInfoForTeam;
import net.caixiaomi.info.model.MatchIntroduceModel;
import net.caixiaomi.info.model.MatchLiveDataEntity;
import net.caixiaomi.info.model.MatchLiveEventEntity;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * Created by suntian on 2018/7/24.
 */

public class MatchDetailLiveAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    private final String ISHA="h";  //h主队，a客队

    private final String MATCH_G="G";   //进球
    private final String MATCH_PG="PG";   //点球
    private final String MATCH_PSM="PSM";   //点球未进
    private final String MATCH_PSG="PSG";   //点球进球
    private final String MATCH_OG="OG";   //乌龙球
    private final String MATCH_RC="RC";   //红牌
    private final String MATCH_YC="YC";   //黄牌
    private final String MATCH_Y2C="Y2C";   //二黄变红
    private final String MATCH_SI="SI";   //换上
    private final String MATCH_SO="SO";   //换下
    private final String MATCH_AS="AS";   //助攻
    private final String MATCH_UNKNOWN="UNKNOWN";   //

    private final String MATCH_POSESSION="posession";

    private MatchIntroduceModel mMatchIntroduceModel;  //全局数据

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MatchDetailLiveAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_MATCH_LIVE0, R.layout.math_live_item_first_layout);
        addItemType(Constants.ITEM_MATCH_LIVE0_NODATA, R.layout.math_live_item_first_layout);
        addItemType(Constants.ITEM_MATCH_LIVE1, R.layout.math_live_item_second_layout);
        addItemType(Constants.ITEM_MATCH_LIVE2, R.layout.math_live_item_third_layout);
        addItemType(Constants.ITEM_MATCH_LIVE3, R.layout.math_live_item_forth_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        try {
            switch (item.getItemType()) {
                case Constants.ITEM_MATCH_LIVE0:
                case Constants.ITEM_MATCH_LIVE0_NODATA:
                    bindFirstView(helper, item);
                    break;
                case Constants.ITEM_MATCH_LIVE1:
                    bindSecondView(helper, item);
                    break;
                case Constants.ITEM_MATCH_LIVE2:
                    bindThirdView(helper, item);
                    break;
                case Constants.ITEM_MATCH_LIVE3:
                    bindForthView(helper, item);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindFirstView(BaseViewHolder helper, MultiItemEntity item){
        if(item.getItemType()==Constants.ITEM_MATCH_LIVE0_NODATA){
            helper.setGone(R.id.match_live_hasdata,false);
            helper.setGone(R.id.match_live_nosdata,true);
        }else{
            helper.setGone(R.id.match_live_hasdata,true);
            helper.setGone(R.id.match_live_nosdata,false);
        }
    }

    private void bindSecondView(BaseViewHolder helper, MultiItemEntity item){

        MatchLiveEventEntity model= (MatchLiveEventEntity) item;
        TextView timerTv=helper.getView(R.id.match_live_item_timer_tv);
        if(TextUtils.equals(model.getIsHa(),"h")){  //主队
            helper.setVisible(net.caixiaomi.info.R.id.match_live_left_group, true);
            helper.setVisible(net.caixiaomi.info.R.id.match_live_right_group, false);
            TextView nameTv=helper.getView(R.id.match_live_left_tv);
            ImageView imgIv=helper.getView(R.id.match_live_left_img);
            ImageView dotIv=helper.getView(R.id.match_live_left_dot_img);
            setMatchLiveData(model,nameTv,imgIv,dotIv,timerTv);
        }else{
            helper.setVisible(net.caixiaomi.info.R.id.match_live_left_group, false);
            helper.setVisible(net.caixiaomi.info.R.id.match_live_right_group, true);
            TextView nameTv=helper.getView(R.id.match_live_right_tv);
            ImageView imgIv=helper.getView(R.id.match_live_right_img);
            ImageView dotIv=helper.getView(R.id.match_live_right_dot_img);
            setMatchLiveData(model,nameTv,imgIv,dotIv,timerTv);
        }

    }

    private void setMatchLiveData(MatchLiveEventEntity entity, TextView nameTv, ImageView imgIv,ImageView dotIv,TextView timerTv){
        try {
        nameTv.setText(entity.getPerson());
        timerTv.setText(entity.getMinute()+"'");
        dotIv.setVisibility(View.INVISIBLE);
        switch (entity.getEventCode()){
            case MATCH_G:   //进球
                imgIv.setBackgroundResource(R.drawable.match_live_goals_img);
                break;
            case MATCH_PG:   //点球
                imgIv.setBackgroundResource(R.drawable.match_live_goals_img);
                dotIv.setVisibility(View.VISIBLE);
                break;
            case MATCH_PSM:  //点球未进
                imgIv.setBackgroundResource(R.drawable.match_live_lostpenalty);
                break;
            case MATCH_PSG:  //点球进球
                imgIv.setBackgroundResource(R.drawable.match_live_goals_img);
                dotIv.setVisibility(View.VISIBLE);
                break;
            case MATCH_OG:  //乌龙球
                imgIv.setBackgroundResource(R.drawable.match_live_owngoals);
                break;
            case MATCH_RC:  //红牌
                imgIv.setBackgroundResource(R.drawable.match_live_redcard);
                break;
            case MATCH_YC:   //黄牌
                imgIv.setBackgroundResource(R.drawable.match_live_yellowcard);
                break;
            case MATCH_Y2C:   //二黄变红
                imgIv.setBackgroundResource(R.drawable.match_live_redden);
                break;
            case MATCH_SI:  //换上
                imgIv.setBackgroundResource(R.drawable.match_live_up);
                break;
            case MATCH_SO:  //换下
                imgIv.setBackgroundResource(R.drawable.match_live_down);
                break;
            case MATCH_AS:  //助攻
                imgIv.setBackgroundResource(R.drawable.match_live_assist);
                break;
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindThirdView(BaseViewHolder helper, MultiItemEntity item){

    }

    private void bindForthView(BaseViewHolder helper, MultiItemEntity item){
        LinearLayout group=helper.getView(R.id.match_live_total_group);
        group.removeAllViews();
        MatchLiveEventEntity model= (MatchLiveEventEntity) item;
        List<MatchLiveDataEntity> list=model.getMatchTotals();
        for (int i=0;i<list.size();i++){
            View view=View.inflate(mContext,R.layout.match_live_total_item_layout,null);
            group.addView(view);
            TextView left= (TextView) view.findViewById(R.id.match_live_total_left_tv);
            TextView right= (TextView) view.findViewById(R.id.match_live_total_right_tv);
            TextView des= (TextView) view.findViewById(R.id.match_live_total_des_tv);
            des.setText(list.get(i).getDataName());
            left.setText(list.get(i).getTeamHData());
            right.setText(list.get(i).getTeamAData());

            double lHostRate=0;
            double lGuestRate=0;
            ProgressBar lHostBar= (ProgressBar) view.findViewById(R.id.host_progress);
            ProgressBar lGuestBar= (ProgressBar) view.findViewById(R.id.guest_progress);

            if(TextUtils.isEmpty(list.get(i).getTeamHData())){
//                left.setText("-");
//                right.setText("-");
                if(TextUtils.equals(list.get(i).getDataCode(),MATCH_POSESSION)||TextUtils.equals(list.get(i).getDataName(),"控球率")){
                    left.setText("0%");
                    right.setText("0%");
                }else{
                    left.setText("0");
                    right.setText("0");
                }
                lHostBar.setProgress(0);
                lGuestBar.setProgress(0);
                lHostBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_oranges_switch_nor_progress));
                lGuestBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_orange_nor_progress));
            }else{
            if(!TextUtils.isEmpty(list.get(i).getTeamHData())){
                lHostRate=Double.parseDouble(list.get(i).getTeamHData());
            }

            if(!TextUtils.isEmpty(list.get(i).getTeamAData())){
                lGuestRate=Double.parseDouble(list.get(i).getTeamAData());
            }

            double lRate=100*lHostRate/(lGuestRate+lHostRate);
            int progress= (int) lRate;

            if(progress==50){
                lHostBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_oranges_switch_progress));
                lGuestBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_orange_progress));
            }else if(progress>50){
                lHostBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_oranges_switch_progress));
                lGuestBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_orange_nor_progress));
            }else{
                lHostBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_oranges_switch_nor_progress));
                lGuestBar.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.bg_orange_progress));
            }

            lHostBar.setProgress(progress);
            lGuestBar.setProgress(100-progress);
                if(TextUtils.equals(list.get(i).getDataCode(),MATCH_POSESSION)){
                    left.setText(progress+"%");
                    right.setText(100-progress+"%");
                }
            }
        }

        if(mMatchIntroduceModel!=null){

            ImageView mIconHost=helper.getView(R.id.icon_host);
            ImageView mIconGuest=helper.getView(R.id.icon_guest);
            TextView mHost=helper.getView(R.id.host);
            TextView mGuest=helper.getView(R.id.guest);

            StringBuilder stringBuilder = new StringBuilder();
            MatchInfoForTeam matchInfo = mMatchIntroduceModel.getMatchInfo();
            RequestOptions options = new RequestOptions();
            options.error(R.mipmap.ic_football);
            options.placeholder(R.mipmap.ic_football);
            Glide.with(mContext).load(matchInfo.getHomeTeamPic()).apply(options).into(mIconHost);
            Glide.with(mContext).load(matchInfo.getVisitingTeamPic()).apply(options).into(mIconGuest);
//            if (!TextUtils.isEmpty(matchInfo.getHomeTeamRank())) {
//                stringBuilder.append("[").append(matchInfo.getHomeTeamRank()).append("]");
//            }
            stringBuilder.append(matchInfo.getHomeTeamAbbr());
            String homeTeam = stringBuilder.toString();
            stringBuilder.setLength(0);
            stringBuilder.append(homeTeam);
            SpannableString spannableString = new SpannableString(stringBuilder);
            mHost.setText(spannableString);

            stringBuilder.setLength(0);
//            if (!TextUtils.isEmpty(matchInfo.getVisitingTeamRank())) {
//                stringBuilder.append("[").append(matchInfo.getVisitingTeamRank()).append("]");
//            }
            stringBuilder.append(matchInfo.getVisitingTeamAbbr());
            String visitTeam = stringBuilder.toString();
            stringBuilder.setLength(0);
            stringBuilder.append(visitTeam);
            spannableString = new SpannableString(stringBuilder);
            mGuest.setText(spannableString);
        }
    }

    public void setMatchDetailData(MatchIntroduceModel model){
        mMatchIntroduceModel=model;
        notifyDataSetChanged();
    }

}
